package com.ruoyi.web.wx.controller;

import com.github.pagehelper.PageHelper;
import com.ruoyi.city.service.ICityService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.conversation.domain.Conversation;
import com.ruoyi.conversation.service.IConversationService;
import com.ruoyi.economize.domain.Economize;
import com.ruoyi.economize.service.IEconomizeService;
import com.ruoyi.evaluate.domain.Evaluate;
import com.ruoyi.evaluate.service.IEvaluateService;
import com.ruoyi.order.domain.Order;
import com.ruoyi.order.domain.request.QrCodeVO;
import com.ruoyi.order.domain.vo.OrderVO;
import com.ruoyi.order.domain.vo.SeachOrderVO;
import com.ruoyi.order.service.IOrderService;
import com.ruoyi.orderdoctor.domain.vo.OrderDoctorVO;
import com.ruoyi.orderdoctor.service.IOrderDoctorService;
import com.ruoyi.user.domain.User;
import com.ruoyi.wx.utils.GetUserUtils;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Api(tags = "订单")
@RestController
@RequestMapping("/wx/order")
public class WxOrderController extends BaseController {

    @Autowired
    private IOrderService orderService;

    @Autowired
    private IEvaluateService evaluateService;

    @Autowired
    private ICityService cityService;

    @Autowired
    private IEconomizeService economizeService;

    @Autowired
    private IConversationService conversationService;
    @Autowired
    private IOrderDoctorService iOrderDoctorService;

    /**
     * 查询【请填写功能名称】列表
     */
    @ApiOperation("查询用户订单列表")
    @PostMapping("/list")
    public AjaxResult list(HttpServletRequest request, @RequestBody OrderVO orderVO) {
        User user = GetUserUtils.getLoginUser(request);
        String orderStatus = orderVO.getOrderStatus();
        String isEvaluate = orderVO.getIsEvaluate();
        List<Integer> size = orderService.selectByUserId(user.getUserId(), orderStatus , isEvaluate);
        PageHelper.startPage(orderVO.getPageNum(), orderVO.getPageSize());
        List<OrderVO> orderVOS = orderService.selectOrderListByUserId(user.getUserId(), orderStatus, isEvaluate);
        TableDataInfo dataTable = getDataTable(orderVOS);
        dataTable.setTotal(size.size());
        return AjaxResult.success(dataTable);
    }

    @ApiOperation(value = "查询订单详情")
    @GetMapping("/findOrderByUserId/{orderId}")
    public AjaxResult findOrderByUserId(@PathVariable String orderId) {
        return AjaxResult.success(orderService.selectOrderByUserId(orderId));
    }

    @ApiOperation("根据专家查询可接订单")
    @PostMapping("/findOrderListByDisease")
    public AjaxResult findOrderListByDisease(HttpServletRequest request, @RequestBody SeachOrderVO seachOrderVO) {
        // 查询分页数据
        TableDataInfo orderListByDisease = orderService.findOrderListByDisease(request, seachOrderVO);
        return AjaxResult.success(orderListByDisease);
    }


    @ApiOperation("忽略订单")
    @PostMapping("/isIgnoreOrder/{orderId}")
    public AjaxResult isIgnoreOrder(HttpServletRequest request, @PathVariable String orderId) {
        User user = GetUserUtils.getLoginUser(request);
        return AjaxResult.success(orderService.isIgnoreOrder(user.getUserId(), orderId));
    }

    /**
     * 查询专家订单列表
     * @param request
     * @return
     */
    @ApiOperation("查询专家订单列表")
    @PostMapping("/findExpertOrder")
    public AjaxResult findExpertOrder(HttpServletRequest request, @RequestBody OrderVO orderVO) {
        User loginUser = GetUserUtils.getLoginUser(request);
        String orderStatus = orderVO.getOrderStatus();
        String isEvaluate = orderVO.getIsEvaluate();
        List<Integer> size = orderService.selectListByExpertId(loginUser.getUserId(), orderStatus, isEvaluate);
        PageHelper.startPage(orderVO.getPageNum(), orderVO.getPageSize());
        List<OrderVO> orderVOS = orderService.selectOrderListByExpertId(loginUser.getUserId(), orderStatus, isEvaluate);
        TableDataInfo dataTable = getDataTable(orderVOS);
        dataTable.setTotal(size.size());
        return AjaxResult.success(dataTable);
    }

    @ApiOperation("专家接单")
    @PostMapping("/takingOrder/{orderId}")
    public AjaxResult takingOrder(HttpServletRequest request, @PathVariable String orderId) {
        User loginUser = GetUserUtils.getLoginUser(request);
        return AjaxResult.success(orderService.takingOrder(loginUser.getUserId(), orderId));
    }

    @ApiOperation("评价订单")
    @PostMapping("/orderEvaluate")
    public AjaxResult orderEvaluate(HttpServletRequest request, @RequestBody Evaluate evaluate) {
        User loginUser = GetUserUtils.getLoginUser(request);
        evaluate.setFromUserId(loginUser.getUserId());
        return AjaxResult.success(evaluateService.insertEvaluate(evaluate));
    }

    @ApiOperation("查询所有省")
    @GetMapping("/getEconomize")
    public AjaxResult getEconomize() {
        return AjaxResult.success(economizeService.selectEconomizeList(new Economize()));
    }

    @ApiOperation("查询省内的所有市")
    @GetMapping("/getCity/{economizeId}")
    public AjaxResult getCity(@PathVariable Long economizeId) {
        return AjaxResult.success(cityService.selectCityListByEconomizeId(economizeId));
    }


    @ApiOperation("根据订单id查询会话列表")
    @GetMapping("/getConversation/{orderId}")
    public AjaxResult getConversation(@PathVariable String orderId) {
        return AjaxResult.success(conversationService.selectConversationByOrderId(orderId));
    }

    @ApiOperation("查询订单评价")
    @GetMapping("getEvaluateByOrderId/{orderId}")
    public AjaxResult getEvaluateByOrderId(@PathVariable String orderId) {
        Evaluate evaluate = new Evaluate();
        evaluate.setOrderId(orderId);
        return AjaxResult.success(evaluateService.selectEvaluate(evaluate));
    }

    @ApiOperation("推荐医生")
    @PostMapping("/insertOrderDoctor")
    public AjaxResult insertOrderDoctorVO(@RequestBody OrderDoctorVO orderDoctorVO) {
        return AjaxResult.success(iOrderDoctorService.insertOrderDoctorVO(orderDoctorVO));
    }

    @ApiOperation("查询会话列表")
    @PostMapping("/getConversation")
    public AjaxResult getConversation(@RequestBody Conversation conversation) {
        return AjaxResult.success(conversationService.selectConversations(conversation));
    }

    @ApiOperation("拒绝关闭会话")
    @PostMapping("/refuseClose")
    public AjaxResult refuseClose(@RequestBody Conversation conversation) {
        return conversationService.refuseClose(conversation);
    }

    @ApiOperation("上传图片聊天")
    @PostMapping("/uploadImageMsg")
    public AjaxResult uploadImageMsg (@RequestPart("file") MultipartFile file) {
        return conversationService.uploadImageMsg(file);
    }

    @ApiOperation("用户结束会话")
    @PostMapping("/endSession")
    public AjaxResult endSession(@RequestBody Order order) {
        return orderService.endSession(order);
    }

    @ApiOperation("查询专家数据")
    @GetMapping("selectExpertData")
    public AjaxResult selectExpertData(HttpServletRequest request) {
        User loginUser = GetUserUtils.getLoginUser(request);
        return orderService.selectExpertData(loginUser);
    }

    @ApiOperation("获取小程序二维码")
    @PostMapping("/qrCode")
    public AjaxResult qrCode(@RequestBody QrCodeVO qrCodeVO) {
        return orderService.qrCode(qrCodeVO);
    }

    @ApiOperation("推送接单消息给用户")
    @PostMapping("/pushMessage")
    public AjaxResult pushMessage(@RequestBody Order order) {
        String orderId = order.getOrderId();
        return orderService.pushMessageUser(orderId);
    }

    @ApiOperation("推送新订单消息给专家")
    @PostMapping("/pushMessageExpert")
    public AjaxResult pushMessageExpert(@RequestBody Order order) {
        return orderService.pushMessageExpert(order);
    }

}
